# descriptive and prescriptive norms and efficacy ----------------------------------------------------
# repeat_absence (attitude (parents should act))

repeat_absence_el <- ols_main(
	outcome = "repeat_absence",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE)

repeat_absence_el_pvals <- get_RI_pvals(
	outcome = "repeat_absence",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")


repeat_absence_ml <- ols_main(
	outcome = "repeat_absence_ml",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE)

repeat_absence_ml_pvals <- get_RI_pvals(
	outcome = "repeat_absence_ml",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")

# village_action(descriptive norm)


village_action_el <- ols_main(
	outcome = "village_action",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE)

village_action_el_pvals <- get_RI_pvals(
	outcome = "village_action",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")


village_action_ml <- ols_main(
	outcome = "village_action_ml",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE)

village_action_ml_pvals <- get_RI_pvals(
	outcome = "village_action_ml",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")





# abs_efficacy 

abs_efficacy_el <- ols_main(
	outcome = "abs_efficacy",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE)

abs_efficacy_el_pvals <- get_RI_pvals(
	outcome = "abs_efficacy",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")


abs_efficacy_ml <- ols_main(
	outcome = "abs_efficacy_ml",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE)

abs_efficacy_ml_pvals <- get_RI_pvals(
	outcome = "abs_efficacy_ml",
	treatment = "absenteeism",
	resample_FE = TRUE,
	block_FE = TRUE,
	audience_size = TRUE,
	cluster_SE = TRUE,
	covariates = NULL,
	the_data = subset(el, respondent_category == "Complier"),
	dosage = FALSE,
	dosage_indicator = FALSE,
	assignment_data = treatment_assignment,
	extract_function = coef,
	analysis_function = ols_main,
	sims = sims,
	lwr_upr_two = "upr")

#setup
control_means <- with(
	el, 
	c(
		"Control Mean",
		round(mean(repeat_absence_ml[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
		round(mean(repeat_absence[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
		round(mean(village_action_ml[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
		round(mean(village_action[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
		round(mean(abs_efficacy_ml[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2),
		round(mean(abs_efficacy[absenteeism == 0 & respondent_category == "Complier"],na.rm = TRUE), 2)
	)
)



pval_lines <- c(
	"RI $p$-values",
	round(repeat_absence_ml_pvals$ri_pvals["absenteeism"],3),
	round(repeat_absence_el_pvals$ri_pvals["absenteeism"],3),
	round(village_action_ml_pvals$ri_pvals["absenteeism"],3),
	round(village_action_el_pvals$ri_pvals["absenteeism"],3),
	round(abs_efficacy_ml_pvals$ri_pvals["absenteeism"],3),
	round(abs_efficacy_el_pvals$ri_pvals["absenteeism"],3)
)

hypothesis_lines <- c(
	"Hypothesis",
	"upr",
	"upr",
	"upr",
	"upr",
	"upr",
	"upr"
)

#make tables
sink("03_tables/ABS_norms_and_efficacy_test.tex")
stargazer(
	... = list(
		repeat_absence_ml$fit,
		repeat_absence_el$fit,
		village_action_ml$fit,
		village_action_el$fit,
		abs_efficacy_ml$fit,
		abs_efficacy_el$fit
	),
	type = "latex",
	p = list(
		repeat_absence_ml_pvals$ri_pvals,
		repeat_absence_el_pvals$ri_pvals,
		village_action_ml_pvals$ri_pvals,
		village_action_el_pvals$ri_pvals,
		abs_efficacy_ml_pvals$ri_pvals,
		abs_efficacy_el_pvals$ri_pvals
	),
	se = list(
		repeat_absence_ml$fit_summary[,"Std. Error"],
		repeat_absence_el$fit_summary[,"Std. Error"],
		village_action_ml$fit_summary[,"Std. Error"],
		village_action_el$fit_summary[,"Std. Error"],
		abs_efficacy_ml$fit_summary[,"Std. Error"],
		abs_efficacy_el$fit_summary[,"Std. Error"]
	),
	keep = "absenteeism",
	omit.stat = c("rsq","f","ser"),
	column.separate = c(2,2,2),
	column.labels = c("Parents should act","Community would intervene","Intervention is effective"),
	table.layout = "=cd#-t-as=n",
	dep.var.labels = c("Midline","Endline",
										 "Midline","Endline",
										 "Midline","Endline"
	),
	dep.var.labels.include = TRUE,
	no.space = T,
	omit = "block_id",
	add.lines = list(
		control_means,
		pval_lines,
		hypothesis_lines,
		c("Block FE",
			"Yes","Yes","Yes",
			"Yes","Yes","Yes", 
			"Yes", "Yes")),
	notes.label = "",
	float = FALSE
	# style = "qje"
)
sink()


